from sqlalchemy import String
from sqlalchemy.orm import mapped_column, Mapped

from core.database import Base


class BaseTable(Base):
    __abstract__ = True

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)

    def dict(self, include=None, exclude=None) -> dict:
        """
        :param include: list 指定该参数时函数将只返回列表下包含的字段
        :param exclude: list 指定该参数时函数将过滤掉列表中包含的字段
        """
        if include is None:
            columns = self.__table__.columns
        else:
            columns = [*filter(lambda col: col.name in include, self.__table__.columns)]
        if exclude is None:
            exclude = []
        _dict = {}
        for c in columns:
            if c.name in exclude:
                continue
            _dict[c.name] = getattr(self, c.name) if hasattr(self, c.name) else None
        return _dict

    def __repr__(self):
        return str(self.dict())


class SystemInfoTable(BaseTable):
    __tablename__ = "sys_info"

    id: Mapped[str] = mapped_column(String(255), primary_key=True, comment="网关编号")
    name: Mapped[str] = mapped_column(String(255), nullable=True, comment="网关名称")
    position: Mapped[str] = mapped_column(String(255), nullable=True, comment="网关安装位置")
    version: Mapped[str] = mapped_column(String(255), nullable=True, comment="系统版本")
    watchEnable: Mapped[bool] = mapped_column(nullable=True, comment="启用看门狗")
    watchIp: Mapped[str] = mapped_column(String(255), nullable=True, comment="看门狗监测ip")
    watchFreq: Mapped[int] = mapped_column(nullable=True, comment="看门狗监测频率")
